﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TDBaseDataLibrary
{
    public class fire
    {
        float speedX;               //X方向速度
        float speedY;               //Y方向速度
        float direct;               //方向
        float speed;
        Color color;
        Vector2 spos;
        Vector2 pos;                //位置
        Texture2D tex;
        Texture2D balltex;
        List<Vector2> tailPoint;
        SplashParticle Particle;
        int state;
        float maxscale;
        float maxexscale;
        float rotate;
        public bool dead;
        TDCharacter e;
        float temptime;
        Random r;
        ContentManager content;
        public fire(ContentManager content, TDCharacter e, Vector2 pos, float direct, float speed, Color color, Texture2D tex)
        {
            temptime = 0;
            this.e = e;
            this.direct = direct;
            this.speed = speed;
            this.tex = tex;
            balltex = content.Load<Texture2D>("ArrowA");
            
            speedX = -(float)Math.Sin(direct) * speed;
            speedY = (float)Math.Cos(direct) * speed;
            this.pos = pos;
            spos = pos;
            tailPoint = new List<Vector2>();
            this.color = color;
            dead = false;
            state = 0;
            maxscale = 1.0f;
             rotate = 0;
             this.content = content;
             
            r = new Random();
        }
        bool HitEnemy(Vector2 Enemypos, Vector2 Start, Vector2 End)
        {
            Vector2 vDStart = Start - Enemypos;
            Vector2 vDEnd = End - Enemypos;
            float f = Vector2.Dot(vDStart, vDEnd);
            if (f < 0)
                return true;
            return false;
        }
        public void Update(GameTime time)
        {
            Vector2 v = new Vector2();
            temptime += (float)time.TotalGameTime.TotalMilliseconds;
            Vector2 startpos = pos;
            
            pos.X += (float)time.ElapsedGameTime.TotalMilliseconds * speedX / 1000;
            pos.Y += (float)time.ElapsedGameTime.TotalMilliseconds * speedY / 1000;
            v = pos;
            rotate += 0.1f;
            Vector2 endpos = pos;
           
            if(state == 0)
            {
                if (Vector2.Distance(v, spos) > 200)
                    state = 1;
                if (HitEnemy(e.m_pos, startpos, endpos))
                {
                    state = 1;
                }
                if (state == 1)
                {
                    endpos = e.m_pos;
                    Particle = new SplashParticle(content, e.m_pos);
                    Particle.Start(time);
                }
                else
                    tailPoint.Add(v);
            }
            else if(state == 1)
            {
                maxscale -= 1.0f/8;
                if (maxscale <= 0)
                {
                    state = 2;
                }
            }
            else if (state == 2)
            {
                Particle.Stop(time);
            }
            if (Particle != null)
                Particle.Update(time);
            if (tailPoint.Count > 8)
            {
                tailPoint.RemoveAt(0);
            }
        }
        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime, SpriteBatch spriteBatch, int OffsetX, int OffsetY)
        {
            if (state != 2)
            {
                Rectangle rtsrc = new Rectangle(0, 0, tex.Width, tex.Height);
                Vector2 vcenter = new Vector2(tex.Width / 2, tex.Height / 2);
                for (int i = 0; i < Math.Min(8, tailPoint.Count); i++)
                {
                    Vector2 v = tailPoint[i];
                    v.X += OffsetX;
                    v.Y += OffsetY;
                    Color ccolor = new Color(color.R,color.G,color.B,maxscale * (((float)i + 1) / 8));
                    spriteBatch.Draw(tex, v, rtsrc, ccolor, 0.0f, vcenter, maxscale * (((float)i + 1) / 8), SpriteEffects.None, 0);
                    if (state == 0&& i == Math.Min(8, tailPoint.Count) - 1)
                        spriteBatch.Draw(balltex, v, rtsrc, Color.White, direct, vcenter, 1.0f, SpriteEffects.None, 0);
                }
            }
            if(Particle != null)
                Particle.Draw(graphics, gameTime, spriteBatch, OffsetX, OffsetY);
        }
    }
}
